package com.cskaoyan.util;

import com.alibaba.druid.util.StringUtils;
import com.cskaoyan.bean.pojo.User;
import com.cskaoyan.bean.pojo.UserExample;
import com.cskaoyan.mapper.UserMapper;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Name : CurrentUserUtils.java
 * @Time : 2021/8/16 19:34
 * @Author : creamycandy
 * @Software : IntelliJ IDEA
 */
public class CurrentUserUtils {

    public static Integer getCurrentUserId(UserMapper userMapper){
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        if(!StringUtils.isEmpty(username)){
            criteria.andUsernameEqualTo(username);
        }
        criteria.andDeletedEqualTo(false);
        List<User> users = userMapper.selectByExample(userExample);
        return users.get(0).getId();
    }

}
